package com.anjubao.parking.municipal.common.config;


import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author anjubao
 * swagger配置
 */
@Configuration
@EnableSwagger2
public class Swagger2Configuration {

	/**
	 * 	默认的排除路径，排除Spring Boot默认的错误处理路径和端点
	 */
	private static final List<String> DEFAULT_EXCLUDE_PATH = Arrays.asList("/error","/actuator/**");
	private static final String BASE_PATH = "/**";

	@Bean
	public Docket createRestApi() {
		ApiInfo apiInfo = new ApiInfoBuilder()
				.title("城市智慧停车平台接口文档")
				.description("城市智慧停车平台接口文档，符合RESTful API。")
				.version("1.0.0")
				.build();
		Predicate<RequestHandler>  basePackage= RequestHandlerSelectors.basePackage("com.anjubao.parking.municipal");

		List<Predicate<String>> basePath = new ArrayList();
		basePath.add(PathSelectors.ant(BASE_PATH));
		List<Predicate<String>> excludePath = new ArrayList<>();
		DEFAULT_EXCLUDE_PATH.forEach(path -> excludePath.add(PathSelectors.ant(path)));

		List<ResponseMessage> globalResponses = Arrays.asList(
				new ResponseMessageBuilder().code(2000).message("操作成功").build(),
				new ResponseMessageBuilder().code(5000).message("操作失败").build());
		return new Docket(DocumentationType.SWAGGER_2)
//				.globalResponseMessage(RequestMethod.GET, globalResponses)
//				.globalResponseMessage(RequestMethod.POST, globalResponses)
//				.globalResponseMessage(RequestMethod.DELETE, globalResponses)
//				.globalResponseMessage(RequestMethod.PUT, globalResponses)
				.apiInfo(apiInfo)
				//屏蔽默认状态码
				.useDefaultResponseMessages(false)
				.select()
				.apis(basePackage)
				.paths(Predicates.and(Predicates.not(Predicates.or(excludePath)), Predicates.or(basePath)))
				.build();
	}
}
